



//
//  SampuxRecordViewController.m
//  Sampux
//
//  Created by rzm on 16/7/12.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxRecordViewController.h"
#import "SampuxRecordView.h"
#import "SPUserOperationModel.h"

@interface SampuxRecordViewController ()

@property (nonatomic, strong) SampuxRecordView *recordView;
@property (nonatomic, strong) SPUserOperationModel *opesModel;
@property (nonatomic, assign) BOOL needRefresh;
@property (nonatomic, strong) NSMutableArray *tempArr;

@end

@implementation SampuxRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestUserOpes:) name:NOTIFICATION_RESPONSE_USER_OPERATE_RECORD_13001 object:nil];
//    [GBInterMaster_CommForDeivce requestUserOpes:0 withTag:1 withDeviceAccount:[[NSUserDefaults standardUserDefaults] objectForKey:@"account"] withUserAccount:self.userCount];

    _recordView = [[SampuxRecordView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _recordView.userCount = self.userCount;
    _recordView.model = self.model;
    [self.view addSubview:_recordView];
    _recordView.backgroundColor = THSystemClearColor;
//    [_recordView.header beginRefreshing];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    SampuxCustomNavController *nav = (SampuxCustomNavController *)self.navigationController;
    nav.titleStr = @"操作记录";
    nav.rightBtn.hidden = NO;
}

- (void)requestUserOpes:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"requestUserOpes == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    _tempArr = [NSMutableArray array];

    if(responseCode == 0x00){
        NSMutableArray* userOpesArr = (NSMutableArray*)[result objectForKey:@"opeRecords"];
        if(userOpesArr.count > 0)
        {
            [_tempArr addObjectsFromArray:userOpesArr];
        } else {
            [_tempArr removeAllObjects];
        }
        [self setDataModel];
    } else {
        [SVProgressHUD showErrorWithStatus:@"获取列表失败"];
//        [MBProgressHUD showError:@"获取列表失败"];
    }
}

- (void)setDataModel
{
    NSMutableArray *opesmodelArr = [NSMutableArray array];
    for (int i = 0; i < _tempArr.count; i ++) {
        GBInterMaster_Bean_UserOpes13001* userOpes = _tempArr[i];
        _opesModel = [SPUserOperationModel alloc];
        _opesModel.requestTime = userOpes.requestTime;
        _opesModel.type = userOpes.type;
        _opesModel.result = userOpes.result;
        _opesModel.responseTime = userOpes.responseTime;
        _opesModel.workMode = userOpes.workMode;
        _opesModel.heatMode = userOpes.heatMode;
        _opesModel.currentWorkMode = userOpes.currentWorkMode;
        _opesModel.currentSetTempe = userOpes.currentSetTempe;
        _opesModel.powerSwitch = userOpes.powerSwitch;
        _opesModel.programDay = userOpes.programDay;
        _opesModel.programOnePoint = userOpes.programOnePoint;
        _opesModel.programOneValue = userOpes.programOneValue;
        _opesModel.programTwoPoint = userOpes.programTwoPoint;
        _opesModel.programTwoValue = userOpes.programTwoValue;
        _opesModel.programThreePoint = userOpes.programThreePoint;
        _opesModel.programThreeValue = userOpes.programThreeValue;
        _opesModel.programFourPoint = userOpes.programFourPoint;
        _opesModel.programFourValue = userOpes.programFourValue;
        [opesmodelArr addObject:_opesModel];
    }
    [self.recordView.dataArr addObjectsFromArray:opesmodelArr];
    self.recordView.dataArr = self.recordView.dataArr;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
